<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('performance_assessments', function (Blueprint $table) {
            $table->id();
            $table->timestamps();

            // 员工信息
            $table->unsignedBigInteger('employee_id');
            $table->string('employee_name', 100)->nullable();
            $table->string('employee_number', 50)->nullable();

            // 考核周期
            $table->string('assessment_period', 20); // monthly, quarterly, semi-annual, annual
            $table->date('period_start');
            $table->date('period_end');
            $table->date('assessment_date');

            // 考核类型
            $table->string('assessment_type', 20); // regular, probation, promotion, annual
            $table->string('template_name', 100)->nullable();

            // 评分信息
            $table->decimal('overall_score', 5, 2)->nullable(); // 综合得分
            $table->string('overall_rating', 20)->nullable(); // excellent, good, average, poor

            // 各维度评分
            $table->decimal('work_quality_score', 5, 2)->nullable(); // 工作质量
            $table->decimal('work_efficiency_score', 5, 2)->nullable(); // 工作效率
            $table->decimal('work_attitude_score', 5, 2)->nullable(); // 工作态度
            $table->decimal('teamwork_score', 5, 2)->nullable(); // 团队协作
            $table->decimal('innovation_score', 5, 2)->nullable(); // 创新能力
            $table->decimal('leadership_score', 5, 2)->nullable(); // 领导力
            $table->json('custom_scores')->nullable(); // 自定义评分项

            // 考核人信息
            $table->unsignedBigInteger('assessor_id')->nullable();
            $table->string('assessor_name', 100)->nullable();
            $table->string('assessor_position', 100)->nullable();

            // 考核结果
            $table->text('strengths')->nullable(); // 优点
            $table->text('weaknesses')->nullable(); // 缺点
            $table->text('improvement_plan')->nullable(); // 改进计划
            $table->text('development_suggestions')->nullable(); // 发展建议
            $table->text('assessor_comments')->nullable(); // 考核人评语

            // 员工反馈
            $table->text('employee_feedback')->nullable(); // 员工反馈
            $table->boolean('employee_acknowledged')->default(false); // 员工确认

            // 状态管理
            $table->string('status', 20)->default('draft'); // draft, submitted, reviewed, approved, completed
            $table->string('approval_status', 20)->nullable(); // pending, approved, rejected

            // 审批信息
            $table->unsignedBigInteger('approved_by')->nullable();
            $table->string('approved_by_name', 100)->nullable();
            $table->datetime('approved_at')->nullable();
            $table->text('approval_comments')->nullable();

            // 关联信息
            $table->unsignedBigInteger('department_id')->nullable();
            $table->string('department_name', 100)->nullable();
            $table->unsignedBigInteger('position_id')->nullable();
            $table->string('position_name', 100)->nullable();

            // 版本控制
            $table->integer('version')->default(1);
            $table->boolean('is_latest')->default(true);

            // 创建者信息
            $table->string('created_by', 100)->nullable();
            $table->string('updated_by', 100)->nullable();

            // 索引
            $table->index(['employee_id', 'period_start', 'period_end'], 'pa_emp_period_idx');
            $table->index(['assessment_date'], 'pa_date_idx');
            $table->index(['assessment_type'], 'pa_type_idx');
            $table->index(['status'], 'pa_status_idx');
            $table->index(['overall_rating'], 'pa_rating_idx');
            $table->index(['assessor_id'], 'pa_assessor_idx');
            $table->index(['department_id'], 'pa_dept_idx');
            $table->index(['position_id'], 'pa_pos_idx');
            $table->index(['is_latest'], 'pa_latest_idx');

            // 外键约束
            $table->foreign('employee_id')->references('id')->on('employees')->onDelete('cascade');
            $table->foreign('assessor_id')->references('id')->on('users')->onDelete('set null');
            $table->foreign('approved_by')->references('id')->on('users')->onDelete('set null');
            $table->foreign('department_id')->references('id')->on('departments')->onDelete('set null');
            $table->foreign('position_id')->references('id')->on('positions')->onDelete('set null');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('performance_assessments');
    }
};
